package com.cndw.kungfu.domain;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.cndw.asyncsync.SyncObject;
import com.cndw.commons.util.BlankUtils;
import com.cndw.kungfu.config.ConstantsRadio;
import com.cndw.kungfu.model.Context;
import com.cndw.kungfu.model.goods.domain.ModeGoodsInlay;
import com.cndw.kungfu.vo.goodsvo.GoodsVO;

/**
 * 玩家道具实体类
 * 
 * @author fantadust
 * @date 2011-4-29 下午04:56:27
 */
public class Goods extends SyncObject {
	/**
	 * 物品id
	 */
	private long id;
	/**
	 * 道具id
	 */
	private int goodsId;
	private int level;
	/**
	 * 玩家id
	 */
	private transient long playerId;
	/**
	 * 穿戴标志
	 */
	private int useNo;
	/**
	 * 装备位置
	 */
	private int position;
	/**
	 * 是否绑定
	 */
	private int binding;
	/**
	 * 来源
	 */
	private int source;
	/**
	 * 堆叠数量
	 */
	private volatile int stackNum;
	/**
	 * 存储类型
	 */
	private volatile int storageType;
	/**
	 * 存储位置
	 */
	private volatile int storagePlace;
	/**
	 * 添加时间
	 */
	private int createTime;
	/**
	 * 加血
	 */
	private int hp;
	/**
	 * 加魔法
	 */
	private int mp;
	/**
	 * 加攻击
	 */
	private int attack;
	/**
	 * 加防御
	 */
	private int defense;
	/**
	 * 命中率
	 */
	private int hitR;
	/**
	 * 闪避率
	 */
	private int dodgeR;
	/**
	 * 幸运
	 */
	private boolean luck;
	/**
	 * 锻造等级
	 */
	private int forgeLevel;
	/**
	 * 打孔数
	 */
	private int holeCounts;
	/**
	 * 宝石属性
	 */
	private String gem;
	/**
	 * 最后更新时间
	 */
	private long updateTime;
	/**
	 * 宝石列表
	 */
	private Map<Integer, Integer> gemMap;
	/**
	 * 物品上次的hashcode
	 */
	private transient int hashcodeKey;

	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public int getGoodsId() {
		return goodsId;
	}

	public void setGoodsId(int goodsId) {
		this.goodsId = goodsId;
	}

	public int getLevel() {
		return level;
	}

	public void setLevel(int level) {
		this.level = level;
	}

	public long getPlayerId() {
		return playerId;
	}

	public void setPlayerId(long playerId) {
		this.playerId = playerId;
	}

	public int getUseNo() {
		return useNo;
	}

	public void setUseNo(int useNo) {
		this.useNo = useNo;
	}

	public int getPosition() {
		return position;
	}

	public void setPosition(int position) {
		this.position = position;
	}

	public int getBinding() {
		return binding;
	}

	public void setBinding(int binding) {
		this.binding = binding;
	}

	public int getSource() {
		return source;
	}

	public void setSource(int source) {
		this.source = source;
	}

	public int getStackNum() {
		return stackNum;
	}

	public void setStackNum(int stackNum) {
		this.stackNum = stackNum;
	}

	public int getStoragePlace() {
		return storagePlace;
	}

	public void setStoragePlace(int storagePlace) {
		this.storagePlace = storagePlace;
	}

	public int getCreateTime() {
		return createTime;
	}

	public void setCreateTime(int createTime) {
		this.createTime = createTime;
	}

	public int getHp() {
		return hp;
	}

	public void setHp(int hp) {
		this.hp = hp;
	}

	public int getMp() {
		return mp;
	}

	public void setMp(int mp) {
		this.mp = mp;
	}

	public int getAttack() {
		return attack;
	}

	public void setAttack(int attack) {
		this.attack = attack;
	}

	public int getDefense() {
		return defense;
	}

	public void setDefense(int defense) {
		this.defense = defense;
	}

	public int getHitR() {
		return hitR;
	}

	public void setHitR(int hitR) {
		this.hitR = hitR;
	}

	public int getDodgeR() {
		return dodgeR;
	}

	public void setDodgeR(int dodgeR) {
		this.dodgeR = dodgeR;
	}

	public int getForgeLevel() {
		return forgeLevel;
	}

	public void setForgeLevel(int forgeLevel) {
		this.forgeLevel = forgeLevel;
	}

	public int getHoleCounts() {
		return holeCounts;
	}

	public void setHoleCounts(int holeCounts) {
		this.holeCounts = holeCounts;
	}

	public String getGem() {

		if (!BlankUtils.isBlank(gemMap)) {
			boolean save = false;
			StringBuilder buffer = new StringBuilder();
			Iterator<Integer> iterator = gemMap.keySet().iterator();
			while (iterator.hasNext()) {
				Integer g = gemMap.get(iterator.next());
				if (g.intValue() != 0 && !save) {
					save = true;
				}
				buffer.append(g);
				if (iterator.hasNext()) {
					buffer.append(",");
				}
			}

			if (save) {
				gem = buffer.toString();
			} else {
				gem = null;
			}
		}

		return gem;
	}

	public void setGem(String gem) {
		this.gem = gem;

		if (!BlankUtils.isBlank(gem)) {

			if (gemMap == null) {
				gemMap = new HashMap<Integer, Integer>();
			} else {
				gemMap.clear();
			}

			String[] skills = gem.split(",");
			for (int i = 0, j = skills.length; i < j; i++) {
				this.gemMap.put(i + 1, Integer.valueOf(skills[i]));
			}
		}
	}

	public Map<Integer, Integer> getGemMap() {
		return gemMap;
	}

	public void updateGem(int hole, int goodsId) {
		if (gemMap == null) {
			gemMap = new HashMap<Integer, Integer>();
			for (int i = 1; i <= 5; i++) {
				gemMap.put(i, 0);
			}
		}
		gemMap.put(hole, goodsId);
	}

	public boolean checkContainGem(int hole) {
		if (gemMap != null) {
			return gemMap.get(Integer.valueOf(hole)).intValue() > 0;
		}
		return false;
	}

	public List<Integer> getGemType() {
		List<Integer> gemType = new ArrayList<Integer>();
		if (!BlankUtils.isBlank(gemMap)) {
			for (Integer gemId : gemMap.values()) {
				if (gemId.intValue() != 0) {
					gemType.add(ModeGoodsInlay.get(gemId).getType());
				}
			}
		}
		return gemType;
	}

	public int getHashcodeKey() {
		return hashcodeKey;
	}

	public void setHashcodeKey(int hashcodeKey) {
		this.hashcodeKey = hashcodeKey;
	}

	public int getStorageType() {
		return storageType;
	}

	public void setStorageType(int storageType) {
		this.storageType = storageType;
	}

	public boolean isLuck() {
		return luck;
	}

	public void setLuck(boolean luck) {
		this.luck = luck;
	}

	public long getUpdateTime() {
		return updateTime;
	}

	public void setUpdateTime(long updateTime) {
		this.updateTime = updateTime;
	}

	/**
	 * 改变堆叠数量
	 * 
	 * @param alert
	 */
	public void alertStackNum(int alert) {
		alertStackNum(alert, false);
	}

	/**
	 * 修改物品数量
	 * 
	 * @param alert
	 * @param silent
	 *            是否为静默模式
	 */
	public synchronized void alertStackNum(int alert, boolean silent) {
		stackNum = stackNum + alert;
		commitSync();// 改变了数量就提交同步
		if (!silent) {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("storagePlace", storagePlace);
			map.put("stackNum", stackNum);
			Context.getTcpServer().broadcast(map, ConstantsRadio.boxChange, playerId);
		}
	}

	/**
	 * 返回物品模型
	 * 
	 * @return
	 */
	public GoodsMode getGoodsMode() {
		return Context.getGoodsServer().getModeById(goodsId);
	}

	/**
	 * 转化vo
	 * 
	 * @return
	 */
	public GoodsVO toGoodsVO() {
		return new GoodsVO(this);
	}

	@Override
	public boolean syncDB() {
		return Context.getSyncServer().syncDB(this);
	}

	public String info() {
		StringBuilder sb = new StringBuilder();
		sb.append("id:").append(id);
		sb.append(" , playerId:").append(playerId);
		sb.append(" , goodsId:").append(goodsId);
		sb.append(" , level:").append(level);
		sb.append(" , stackNum:").append(stackNum);
		sb.append(" , forgeLevel:").append(forgeLevel);
		sb.append(" , storageType:").append(storageType);
		sb.append(" , storagePlace:").append(storagePlace);

		return sb.toString();
	}

}
